package com.ruoyi.hr.service.impl.attendempmanage.attendChange;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.hr.mapper.attendempmanage.attendChange.AttendChangeMapper;
import com.ruoyi.hr.service.attendempmanage.attendChange.AttendChangeService;
import com.ruoyi.hr.util.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuanwei
 * @explain 考勤变动
 * @date 2020/4/26
 */
@Service
public class AttendChangeSerivceImpl implements AttendChangeService {

    @Autowired
    private AttendChangeMapper attendChangeMapper;

    //考勤信息变动

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findInfoChangeList(Map<String, Object> map) {
        return attendChangeMapper.findInfoChangeList(utils.addAuthority(map));
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public int remove(String ids) {
        String[] idSplit = ids.split(",");
        int num = 0;
        for (String id : idSplit) {
            int count = attendChangeMapper.getCheck(id);
            if (count > 0) {
                return 0;
            }
            num = attendChangeMapper.remove(id);
            if (num == 0) {
                return 0;
            }
        }
        return num;
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public int save(Map<String, Object> map) {
        int num = 0;
        if (map.containsKey("id")) {
            num = attendChangeMapper.update(map);
        } else {
            num = attendChangeMapper.insert(map);
        }
        return num;
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public Map<String, Object> findPerson(String badge) {
        Map<String, Object> map = attendChangeMapper.findPerson(badge);
        if (map == null) {
            map = new HashMap<String, Object>();
            map.put("msg", "error");
        } else {
            map.put("msg", "success");
        }
        return map;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findAtEmpType() {
        return attendChangeMapper.findAtEmpType();
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public Map<String, Object> findInfoChange(String id) {
        return attendChangeMapper.findInfoChange(id);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findInfoChangeHistory(Map<String, Object> map) {
        return attendChangeMapper.findInfoChangeHistory(utils.addAuthority(map));
    }

    //考勤状态变动

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findInfoStatusList(Map<String, Object> map) {
        return attendChangeMapper.findInfoStatusList(utils.addAuthority(map));
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public Map<String, Object> findInfoStatus(String id) {
        return attendChangeMapper.findInfoStatus(id);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findAtStatus() {
        return attendChangeMapper.findAtStatus();
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public int saveStatus(Map<String, Object> map) {
        int num = 0;
        if (map.containsKey("id")) {
            num = attendChangeMapper.updateStatus(map);
        } else {
            num = attendChangeMapper.insertStatus(map);
        }
        return num;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findInfoStatusHistory(Map<String, Object> map) {
        return attendChangeMapper.findInfoStatusHistory(utils.addAuthority(map));
    }
}
